<?php 
$orgAccount = $this->Session->read('org_account');
?>

<form action="<?php echo $this->Html->url(array('action' => 'category')); ?>" method ="post" id="category-form" class="form form-horizontal">
    <?php
    $flash = $this->Session->flash();

    if ($flash) {
        echo "<div class='alert alert-success'>"
        . "<button class='close' data-dismiss='alert'><i class='fa fa-times'></i></button>"
        . "$flash"
        . "</div>";
    }
    ?>

    <div class="row">
        <div class="col-lg-12">
            <h5 class="text-info font-bold">Category Definition</h5>
        </div>

        <div class="col-lg-12">
            <div class="form-group">
                <div class="col-lg-5 control-label">
                    Name:
                </div>
                <div class="col-lg-7 controls">
                    <input type="text" id="category_name" name="OrganisationMemberCategory[name]" required="" class="form-control input-sm input-s" value='<?php echo isset($category) ? $category['OrganisationMemberCategory']['name'] : ""; ?>' />
                    <input type="hidden" id="category_id" name="OrganisationMemberCategory[id]" required="" class="form-control input-sm input-s" value='<?php echo isset($category) ? $category['OrganisationMemberCategory']['id'] : ""; ?>' />
                    <input type="hidden" name="OrganisationMemberCategory[organisation_id]" required="" class="form-control input-sm input-s" value='<?php echo isset($category) ? $category['OrganisationMemberCategory']['organisation_id'] : $orgAccount['Organisation']['id']; ?>' />
                </div>
            </div>
            
            <div class="form-group">
                <div class="col-lg-5 control-label">
                    Slug:
                </div>
                <div class="col-lg-7 controls">
                    <input type="text" id="category_slug" name="OrganisationMemberCategory[slug]" required="" class="form-control input-sm input-s" value='<?php echo isset($category) ? $category['OrganisationMemberCategory']['slug'] : ""; ?>' />
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-5 control-label">
                    Description:
                </div>
                <div class="col-lg-7 controls">
                    <input type="text" name="OrganisationMemberCategory[description]" class="form-control input-sm input-s-lg" value='<?php echo isset($category) ? $category['OrganisationMemberCategory']['description'] : ""; ?>' />
                </div>
            </div>
            
            <div class="form-group">
                <div class="col-lg-5 control-label">
                    ID Prefix:
                </div>
                <div class="col-lg-7 controls">
                    <input type="text" name="OrganisationMemberCategory[id_prefix]" class="form-control pull-left input-sm input-s-sm" value='<?php echo isset($category) ? $category['OrganisationMemberCategory']['id_prefix'] : ""; ?>' />
                    <span class="muted pull-left m-l text-xs" style="line-height: 30px;"><i>e.g. ABC</i></span>
                </div>
            </div>
            
            <div class="form-group">
                <div class="col-lg-5 control-label">
                    ID Suffix:
                </div>
                <div class="col-lg-7 controls">
                    <input type="text" name="OrganisationMemberCategory[id_suffix]" class="form-control pull-left input-sm input-s-sm" value='<?php echo isset($category) ? $category['OrganisationMemberCategory']['id_suffix'] : ""; ?>' />
                    <span class="muted pull-left m-l text-xs" style="line-height: 30px;"><i>e.g. ABC</i></span>
                </div>
            </div>
            
            <div class="form-group">
                <div class="col-lg-5 control-label">
                    ID Next Increment:
                </div>
                <div class="col-lg-7 controls">
                    <input type="text" name="OrganisationMemberCategory[id_next_increment]" class="form-control pull-left input-sm input-s-xs" value='<?php echo isset($category) ? $category['OrganisationMemberCategory']['id_next_increment'] : ""; ?>' />
                </div>
            </div>
            
            <div class="form-group">
                <div class="col-lg-5 control-label">
                    Set as Default:
                </div>
                <div class="col-lg-7 controls">
                    <label class='switch'>
                        <input type='checkbox' name='OrganisationMemberCategory[default]' value='1' <?php echo isset($category) && $category['OrganisationMemberCategory']['default'] == 1 ? "checked" : ""; ?>  />
                        <span></span>
                    </label>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12">
            <h5 class="text-info font-bold">Category Settings</h5>
        </div>

        <div class="col-lg-12">
            <?php
            $i = 0;
            foreach ($settings as $setting) {
                ?>

                <div class="form-group">
                    <div class="col-lg-5 control-label">
                        <?php echo $setting['MemberCategorySetting']['description']; ?>
                    </div> 
                    <div class="col-lg-7 controls">
                        <?php
                        $name = $setting['MemberCategorySetting']['name'];
                        $value = $setting['MemberCategorySetting']['default'];
                        $id = "";

                        if( isset($category) ) {
                            foreach($category['OrganisationMemberCategorySetting'] as $cat_setting) {
                                if( $cat_setting['member_category_setting_id'] == $setting['MemberCategorySetting']['id'] ) {
                                    $value = $cat_setting['value'];
                                    $id = $cat_setting['id'];
                                }
                            }
                        }

                        switch ($setting['MemberCategorySetting']['type']) {
                            case 'text':
                            case 'email':
                                echo "<input type='text' name='OrganisationMemberCategorySetting[$i][value]' class='form-control input-sm input-s' value='$value' />";
                                break;

                            case 'number':
                                echo "<input type='number' name='OrganisationMemberCategorySetting[$i][value]' class='form-control input-sm input-s-xs' value='$value' />";
                                break;

                            case 'flag':
                                $checked = $value == 1 ? "checked=checked" : "";
                                echo "<label class='switch'>"
                                . "<input type='checkbox' name='OrganisationMemberCategorySetting[$i][value]' value='1' $checked />"
                                . "<span></span>"
                                . "</label>";
                                break;

                            case 'date':
                            case 'datetime':
                                $type = $setting['MemberCategorySetting']['type'];
                                echo "<input type='text' name='OrganisationMemberCategorySetting[$i][value]' class='form-control datepicker-input input-sm input-s' value='$value' data-date-format='yyyy-mm-dd' />";
                                break;
                        }

                        echo "<input type='hidden' name='OrganisationMemberCategorySetting[$i][member_category_setting_id]' value='{$setting['MemberCategorySetting']['id']}' />";
                        echo "<input type='hidden' name='OrganisationMemberCategorySetting[$i][organisation_member_category_id]' value='" . (isset($category) ? $category['OrganisationMemberCategory']['id'] : '') . "' />";
                        echo "<input type='hidden' name='OrganisationMemberCategorySetting[$i][organisation_id]' value='" . (isset($category) ? $category['OrganisationMemberCategory']['organisation_id'] : $orgAccount['Organisation']['id']) . "' />";
                        echo "<input type='hidden' name='OrganisationMemberCategorySetting[$i][id]' value='$id' />";

                        $i++;
                        ?>
                    </div>
                </div>
            <?php } ?>
        </div>
    </div>

    <div class="form-group m-t-lg">
        <div class="col-lg-5 control-label">
            &nbsp;
        </div> 
        <div class="col-lg-7 controls p-sm">
            <input type="submit" class="btn btn-sm btn-info" value="Save Changes" />
            <input type="button" id="cancel-btn" class="btn btn-sm btn-white" value="Cancel" />
        </div>
    </div>
</form>